<?php
namespace app\manage\controller;

use think\facade\Db;
class Syslog extends Base
{
    public function loginRecord() {
        $param['search'] = input('post.search');
        $param['role'] = input('post.role');
        $curr_page = input('post.pageIndex',1);
        $perpage = input('post.pageSize',10);
        $date = input('post.date',[]);

        $where = [
            ['a.del','=',0]
        ];
        if($param['search']) {
            $where[] = ['a.realname|a.username','like',"%{$param['search']}%"];
        }
        if($param['role']) {
            $where[] = ['t.role','like',$param['role']];
        }
        if(!empty($date)) {
            $where[] = ['t.create_time','>',strtotime($date[0])];
            $where[] = ['t.create_time','<',strtotime($date[1] . ' 23:59:59')];
        }
        try {
            $count = Db::table('mp_syslog')->alias('t')
                ->join('mp_admin a','t.admin_id=a.id','left')
                ->where($where)->count();
            $list = Db::table('mp_syslog')->alias('t')
                ->join('mp_admin a','t.admin_id=a.id','left')
                ->where($where)
                ->limit(($curr_page - 1)*$perpage,$perpage)
                ->order(['t.create_time'=>'DESC'])
                ->field('t.create_time,t.login_type,t.region AS province,t.ip,a.id,a.role,a.username,a.realname,a.avatar')
                ->select();
            $rolelist = Db::table('mp_auth_role')->where('del','=',0)->field('id,title')->select();
        }catch (\Exception $e) {
            return ajax('SQL错误: ' . $e->getMessage(),-1);
        }
        $ret = [
            'adminlist' => $list,
            'rolelist' => $rolelist,
            'count' => $count
        ];
        return ajax($ret);

    }

}